<?php
class Shipping_ShipTypeCustomerController extends Zend_Controller_Action {
	
	public function indexAction() {
		if ($this->_request->isPost ()) {
			$page = $this->_request->getParam ( 'page', 1 );
			$pageSize = $this->_request->getParam ( 'pageSize', 20 );
			$st_code = $this->_request->getParam ( 'st_code', '' );
			$customer_id = $this->_request->getParam ( 'customer_id', '' );
			$condition = array ('st_code' => $st_code, 'customer_id' => $customer_id );
			
			$shipTypeList = Shipping_Service_ShipTypeCustomer::getByCondition ( $condition, '*', 'st_code', $page, $pageSize );
			$shipTypeList = Shipping_Service_ShipTypeCustomer::getInfo ( $shipTypeList );
			
			$count = Shipping_Service_ShipTypeCustomer::getByCondition ( $condition, 'count(*)' );
			$json = array ('list' => $shipTypeList, 'count' => $count );
			echo json_encode ( $json );
			exit ();
		
		} else {
			
			$stClassCodeList = Shipping_Service_ShipTypeCustomer::getStClassCode ();
			$warehouseList = Warehouse_Service_Warehouse::getByCondition ();
			$statusList = Shipping_Service_ShipTypeCustomer::getStatusList ();
			$shipType = Shipping_Service_ShipType::getAll ();
			$this->view->stClassCodeList = $stClassCodeList;
			$this->view->warehouseList = $warehouseList;
			$this->view->statusList = $statusList;
			$this->view->shipType = $shipType;
			echo Cff::renderTpl ( "shipping/shiptypecustomer/index.tpl", "layout" );
		}
	}
	public function getcustomerAction() {
		$customer_id = $this->_request->getParam ( 'customer_id_bind', '' );
		$page = $this->_request->getParam ( 'page', 1 );
		$pageSize = $this->_request->getParam ( 'pageSize', 20 );
		$cond = array ();
		
		if (! empty ( $customer_id )) {
			$cond ['customer_id'] = $customer_id;
		}
		$page = $this->_request->getParam ( 'page', 1 );
		$pageSize = $this->_request->getParam ( 'pageSize' );
		
		$orderby = 'customer_id';
		$result = Customer_Service_Customer::getByCond ( $cond, '*', $orderby, $pageSize, ($page - 1) * $pageSize );
		$total = Customer_Service_Customer::getByCond ( $cond, 'count(*)', $orderby, 0, 0 );
		$data = array ('ask' => 0 );
		if ($total > 0) {
			$data = array ('total' => $total, 'result' => $result, 'ask' => 1 );
		}
		die ( Zend_Json::encode ( $data ) );
	}
	
	public function bindAction() {
	    $lang = Zend_Registry::get('lang');
		$return = array ();
		$st_code_bind = $this->_request->getParam ( 'st_code_bind', 0 );
		$st_bind_type = $this->_request->getParam ( 'st_bind_type', '' );
		$customerid = $this->_request->getParam ( 'customerid', '' );
		$date = date ( "Y-m-d:H:i:s" );
		$rowadd = false;
		if ($st_bind_type == 1) {
			if ($customerid) {
				$customerBalance = new Customer_Model_CustomerBalance ();
				$dbAdapter = $customerBalance->getAdapter ();
				$dbAdapter->beginTransaction ();
				try {
					foreach ( $customerid as $val ) {
						$condition = array ('st_code' => $st_code_bind, 'customer_id' => $val );
						$isExist = Shipping_Service_ShipTypeCustomer::getByCondition ( $condition, '*', 'st_code', 0, 0 );
						if (! $isExist) {
							$bindData = array ('st_code' => $st_code_bind, 'customer_id' => $val, 'stc_add_time' => $date );
							if (! Shipping_Service_ShipTypeCustomer::add ( $bindData )) {
								$dbAdapter->rollback ();
								$rowadd = false;
							}
						}
					
					}
					$dbAdapter->commit ();
					$rowadd = true;
				} catch ( Exception $e ) {
					$dbAdapter->rollback ();
					$rowadd = false;
				}
			
			}
		} else {
			$customerBalance = new Customer_Model_CustomerBalance ();
			$dbAdapter = $customerBalance->getAdapter ();
			$dbAdapter->beginTransaction ();
			try {
				$condition = array ('st_code' => $st_code_bind, 'customer_id' => '*' );
				$isExist = Shipping_Service_ShipTypeCustomer::getByCondition ( $condition, '*', 'st_code', 0, 0 );
				if (! $isExist) {
					$bindData = array ('st_code' => $st_code_bind, 'customer_id' => '*', 'stc_add_time' => $date );
					if (! Shipping_Service_ShipTypeCustomer::add ( $bindData )) {
						$dbAdapter->rollback ();
						$rowadd = false;
					}
				}
				
				$dbAdapter->commit ();
				$rowadd = true;
			} catch ( Exception $e ) {
				$dbAdapter->rollback ();
				$rowadd = false;
			}
		}
		
		if ($rowadd) {
			$return ['ask'] = true;
			$return ['msg'] = $lang['operation'].$lang['success'];
		} else {
			$return ['ask'] = false;
			$return ['msg'] = $lang['operation'].$lang['fail'];
		}
		echo Zend_Json::encode ( $return );
		exit ();
	
	}
	
	public function infoAction() {
		$st_id = $this->_request->getParam ( 'st_id', 0 );
		try {
			$shipType = Shipping_Service_ShipType::getAll ();
			
			$return = array ('ask' => true, 'list' => $shipType );
		} catch ( Exception $e ) {
			$return = array ('ask' => false, 'list' => null );
		}
		echo json_encode ( $return );
		exit ();
	}
	
	public function editAction() {
	
	}
	
	public function batchdeleteAction() {
	    $lang = Zend_Registry::get('lang');
		$return = array ();
		$stc_id = $this->_request->getParam ( 'stc_id', 0 );
		$delrow = false;
		foreach ( $stc_id as $val ) {
			$delrow = Shipping_Service_ShipTypeCustomer::delete ( $val );
		}
		if ($delrow) {
			$return ['ask'] = true;
			$return ['msg'] = $lang['operation'].$lang['success'];
		} else {
			$return ['ask'] = false;
			$return ['msg'] = $lang['operation'].$lang['fail'];
		}
		echo Zend_Json::encode ( $return );
		exit ();
	}
	public function deleteAction() {
	    $lang = Zend_Registry::get('lang');
		$return = array ();
		$stc_id = $this->_request->getParam ( 'stc_id', 0 );
		if (Shipping_Service_ShipTypeCustomer::delete ( $stc_id )) {
			$return ['ask'] = true;
			$return ['msg'] = $lang['operation'].$lang['success'];
		} else {
			$return ['ask'] = false;
			$return ['msg'] = $lang['operation'].$lang['fail'];
		}
		echo Zend_Json::encode ( $return );
		exit ();
	}
}